' An improved version of the PagingBar user control
' UNCOMMENT NEXT LINE IF YOU WANT TO ENFORCE
' PARTIAL CACHING VIA AN ATTRIBUTE
'<PartialCaching(5, "none", "PageNumber", "")> _
Public MustInherit Class PagingBar
Inherits System.Web.UI.UserControl
Implements IPostBackDataHandler
Protected WithEvents btnLast As System.Web.UI.WebControls.Button
Protected WithEvents btnNext As System.Web.UI.WebControls.Button
Protected WithEvents btnPrevious As System.Web.UI.WebControls.Button
Protected WithEvents litPageNumber As System.Web.UI.WebControls.Literal
Protected WithEvents btnFirst As System.Web.UI.WebControls.Button
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
' this is a public event exposed to the outside
Public Event PageChanged(ByVal sender As Object, ByVal e As EventArgs)
' this is a public property exposed to the outside
' it is just a wrapper on a property of the litPageNumber constituent control
Public Property PageNumber() As Integer
Get
Return CInt(litPageNumber.Text.TrimStart)
End Get
Set(ByVal Value As Integer)
If Value >= 1 And Value <= PageCount AndAlso Value <> PageNumber Then
' this is where the PageNumber property is held
litPageNumber.Text = Value.ToString
' update other controls' state
UpdateButtonState()
' Let the parent form know that the page has changed
RaiseEvent PageChanged(Me, EventArgs.Empty)
End If
End Set
End Property
' The PageCount property
' unlike PageNumber, this value is held in the ViewState bag
Private m_PageCount As Integer = -1
Public Property PageCount() As Integer
Get
If m_PageCount < 0 Then
Dim objValue As Object = Me.ViewState("PageCount")
If Not objValue Is Nothing Then
m_PageCount = CInt(objValue)
Else
' this is the default value
m_PageCount = 100
End If
End If
Return m_PageCount
End Get
Set(ByVal Value As Integer)
If Value >= 1 Then
m_PageCount = Value
UpdateButtonState()
' Save in the page's viewstate.
Me.ViewState("PageCount") = Value
End If
End Set
End Property
' Update buttons' state
Sub UpdateButtonState()
btnFirst.Enabled = (PageNumber > 1)
btnPrevious.Enabled = (PageNumber > 1)
btnNext.Enabled = (PageNumber < PageCount)
btnLast.Enabled = (PageNumber < PageCount)
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
' react to clicks on navigational buttons
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
PageNumber = 1
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
PageNumber = PageCount
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
PageNumber -= 1
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
PageNumber += 1
End Sub
' this method is invoked when a postback occurs
Public Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData
' we don't need to do anything special here
Return False
End Function
' this method is invoked after all the controls in the parent form
' have processed the LoadPostData method, and only if the
' control returned True in that method
' (hence it will never been invoked in this demo)
Public Sub RaisePostDataChangedEvent() Implements System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent